package com.aplink.generic.util;

import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.AsyncTask;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class CityAsyncTask extends AsyncTask<String, String, StringBuilder> {
    Activity act;
    double latitude;
    double longitude;

    public CityAsyncTask( Activity act, double latitude, double longitude ) {
        // TODO Auto-generated constructor stub
        this.act = act;
        this.latitude = latitude;
        this.longitude = longitude;
    }

    @Override
    protected StringBuilder doInBackground( String... arg0 ) {
        final StringBuilder _address = new StringBuilder();
        final Geocoder geocoder = new Geocoder( this.act, Locale.getDefault() );
        try {
            final List<Address> addresses = geocoder.getFromLocation( this.latitude,
                    this.longitude, 1 );
            Address address = null;
            address = addresses.get( 0 );
            _address.append( "Address: " ).append( address.getAddressLine( 0 ) ).append( "\n" );
            _address.append( "City: " ).append( address.getLocality() ).append( "\n" );
            _address.append( "Sub-Admin Area: " ).append( address.getSubAdminArea() ).append( "\n" );
            _address.append( "Country: " ).append( address.getCountryName() ).append( "\n" );

        } catch ( final IOException e ) {
            e.printStackTrace();
        }
        return _address;
    }

    @Override
    protected void onPostExecute( StringBuilder result ) {
        // TODO Auto-generated method stub
        super.onPostExecute( result );
    }

}
